#region --- License & Copyright Notice ---
/*
Copyright (c) 2005-2011 Jeevan James
All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#endregion

using System;
using System.IO;
using System.Windows.Forms;
using Id3.Info;
using Id3.Info.Amazon;

namespace Id3.Net.Samples.BatchEditor
{
    public partial class FrmMain : Form
    {
        private string _mp3Directory;
        private Mp3File _mp3;
        private Id3Tag _tag;
        private Id3TagSubset _tagSubset;

        public FrmMain()
        {
            InitializeComponent();
        }

        private void tbtnSelectMp3Directory_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(ttxtMp3Directory.Text))
            {
                MessageBox.Show("The MP3 directory you specified does not exist");
                return;
            }

            _mp3Directory = ttxtMp3Directory.Text;

            string[] mp3Files = Directory.GetFiles(_mp3Directory, "*.mp3");
            lbxFiles.BeginUpdate();
            try
            {
                lbxFiles.Items.Clear();
                foreach (string mp3File in mp3Files)
                    lbxFiles.Items.Add(Path.GetFileNameWithoutExtension(mp3File));
            }
            finally
            {
                lbxFiles.EndUpdate();
            }
        }

        private void tbtnMyMusic_Click(object sender, EventArgs e)
        {
            ttxtMp3Directory.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
        }

        private void lbxFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_tag != null && _tagSubset != null && _tagSubset.Modified)
            {
                _tagSubset.SaveToTag(_tag);
                _mp3.WriteTag(_tag, WriteConflictAction.Replace);
            }

            _tag = null;
            if (_mp3 != null)
            {
                _mp3.Dispose();
                _mp3 = null;
            }

            string filename = Path.Combine(_mp3Directory, lbxFiles.SelectedItem + ".mp3");
            _mp3 = new Mp3File(filename);
            _tag = _mp3.GetTag(2, 3);
            _tagSubset = new Id3TagSubset(_tag);
            grdId3Properties.SelectedObject = _tagSubset;
        }

        private void FrmMain_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = false;
            if (e.Control && !e.Alt && !e.Shift)
            {
                int newIndex = lbxFiles.SelectedIndex;
                if (e.KeyCode == Keys.Up)
                    newIndex -= 1;
                else if (e.KeyCode == Keys.Down)
                    newIndex += 1;
                else
                    return;
                if (newIndex < 0 || newIndex >= lbxFiles.Items.Count)
                    return;
                lbxFiles.SelectedIndex = newIndex;
                e.Handled = true;
            }
        }

        private void tbtnInfoLookup_Click(object sender, EventArgs e)
        {
            InfoProvider provider = new AmazonInfoProvider();
            if (!provider.MeetsInputCriteria(_tag))
            {
                MessageBox.Show("Tag is either null or does not contain the required frames");
                return;
            }

            Id3Tag[] results = provider.GetInfo(_tag, InfoProviderInputs.Default);

            lvInfoResults.BeginUpdate();
            try
            {
                lvInfoResults.Items.Clear();
                foreach (Id3Tag result in results)
                {
                    ListViewItem listItem = new ListViewItem(result.Album.Value);
                    listItem.SubItems.Add(result.Artists.Value);
                    listItem.SubItems.Add(result.PaymentUrl.Url);
                    listItem.SubItems.Add(result.Publisher.Value);
                    listItem.SubItems.Add(result.Year.Value);
                    listItem.Tag = result;
                    lvInfoResults.Items.Add(listItem);
                }
            }
            finally
            {
                lvInfoResults.EndUpdate();
            }
        }

        private void lvInfoResults_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvInfoResults.SelectedItems.Count == 0)
                picAlbumArt.Image = null;
            else
            {
                ListViewItem listItem = lvInfoResults.SelectedItems[0];
                var tag = (Id3Tag)listItem.Tag;
                if (tag.Pictures.Count > 0)
                    picAlbumArt.Image = tag.Pictures[0].GetPicture();
            }
        }
    }
}